// 管理员相关操作权限

function actionFormatter(value, row, index) {
    return [
        '<button class="mcdetail btn btn-primary btn-sm col-md-6">',
        '商户详情',
        '</button>',
    ].join('');
}

window.actionEvents = {
    'click .mcdetail': function (e, value, row, index) {

        $('#mcinfo').empty();
        var sh = ""
        if(row.Status == -1){
            sh =       '<button class="btn btn-primary btn-sm col-md-6">' +
                        '认证通过' +
                        '</button>' 
        }

        var s =  $('#mcinfo').append($(
                                '<table class="table table-user-information">' + 
                                    '<tbody>' +
                                        '<tr>' +
                                            '<td>名称:</td>' +
                                            '<td>'+ row.Name +'</td>' + 
                                        '</tr>' + 
                                        '<tr>' + 
                                            '<td>管理员:</td>' + 
                                            '<td>'+ row.Administor +'</td>' + 
                                        '</tr>' + 
                                        '<tr>' + 
                                            '<td>电话:</td>' + 
                                            '<td>'+ row.Tel +'</td>' + 
                                        '</tr>' + 
                                        '<tr>' + 
                                            '<tr>' + 
                                                '<td>地址:</td>' + 
                                                '<td>'+ row.Addr +'</td>' + 
                                            '</tr>' + 
                                            '<tr>' + 
                                                '<td>主营商品</td>' + 
                                                '<td>'+ row.MainProducts +'</td>' + 
                                            '</tr>' + 
                                        '</tr>' +
                                    '</tbody>' +
                                '</table>' + 
                                sh +
                                '<div class="row" id="mc-items">' +
                                    '<table id="admin-mc-items-table"'+
                                            'data-toggle="table">'+
                                        '<thead>'+
                                        '<tr>'+
                                            '<th data-field="Name">商品名称</th>'+
                                            '<th data-field="Price">价格</th>'+
                                            '<th data-field="Num">商品数量</th>'+
                                            '<th data-field="Status">商品状态</th>'+
                                            '<th data-field="操作" data-formatter="actionMcFormatter" data-events="actionMcEvents">Action</th>'+
                                        '</tr>'+
                                        '</thead>'+
                                    '</table>' +
                                    '<div class="row" id="preview">' +
                                    '</div>' +
                                '</>')); //end append
        console.log(s);
        if(row.Status == -1) {
            $('#mcinfo button').click(function(event) {
                /* Act on the event */
                $.ajax({
                    url: '/mc/approve',
                    type: 'GET',
                    data: {mcId: row.Id},
                })
                .done(function() {
                    window.location.href='/';
                })
                .fail(function() {
                    alert("更新商户信息失败！");
                });
                
            });
        }

        var $table = $('#admin-mc-items-table')
        $table.bootstrapTable({});
        //加载商户的商品信息
        $.ajax({
            url: '/seller/allitems',
            type: 'GET',
            dataType: 'json',
            data: {userid: row.Administor},
        })
        .done(function(data) {
            $table.bootstrapTable('load', data);
            $table.bootstrapTable('refreshOptions', {
              exportDataType: 'all'
            });
            console.log("success" + row.Administor);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        

    }// end click function


};

//商户相关管理权限

function actionMcFormatter(value, row, index) {
    if(row.Status == 0){
        return [
            '<button class="itemdetail btn btn-primary btn-sm col-md-6">',
            '商品详情',
            '</button>',
            '<button class="itemdapprove btn btn-primary btn-sm col-md-6">',
            '审核通过',
            '</button>',
        ].join('');
    }
    return [
        '<button class="itemdetail btn btn-primary btn-sm col-md-6">',
        '商品详情',
        '</button>',
    ].join('');
}

window.actionMcEvents = {
    'click .itemdetail': function (e, value, row, index) {  
        $('#preview').empty();
        var id = row.Id;
        $('#preview').append($('<div class="row">' +
            '<div class="col-md-offset-4 col-md-4">' +
                '<iframe class="preview-iframe" src="/pcpreview?id=' + id +'"></iframe>' +
            '</div>' +
            '</div>'));

    },// end click function
    'click .itemdapprove': function (e, value, row, index) {
        $.ajax({
            url: '/approve/item',
            type: 'GET',
            data: {id: row.Id},
        })
        .done(function() {
            var $table = $('#admin-mc-items-table')
            //加载商户的商品信息
            $.ajax({
                url: '/seller/allitems',
                type: 'GET',
                dataType: 'json',
                data: {userid: row.SallerId},
            })
            .done(function(data) {
                $table.bootstrapTable('load', data);
                $table.bootstrapTable('refreshOptions', {
                  exportDataType: 'all'
                });
                console.log("success" + row.SallerId);
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
        });
        
    }
};